﻿using System;
using System.Management.Automation;

namespace BO.Management.PowerShell
{
    public sealed class BOErrorManagement
    {
        public static ErrorRecord GetErrorRecord(BOErrorCode errorCode, String message)
        {
            return BOErrorManagement.GetErrorRecord(errorCode, message, null);
        }

        public static ErrorRecord GetErrorRecord(BOErrorCode errorCode, String message, Exception innerException)
        {
            Exception ex = null;

            if (innerException == null)
                ex = new Exception(message);
            else
                ex = new Exception(message, innerException);


            switch (errorCode)
            {
                case BOErrorCode.GenericError:
                    return new ErrorRecord(ex, Enum.GetName(typeof(BOErrorCode), errorCode), ErrorCategory.NotSpecified, null);
                case BOErrorCode.InvalidArgument:
                    return new ErrorRecord(ex, Enum.GetName(typeof(BOErrorCode), errorCode), ErrorCategory.InvalidArgument, null);
                case BOErrorCode.OpenCmsSession:
                    return new ErrorRecord(ex, Enum.GetName(typeof(BOErrorCode), errorCode), ErrorCategory.OpenError, null);
                case BOErrorCode.TimeOut:
                    return new ErrorRecord(ex, Enum.GetName(typeof(BOErrorCode), errorCode), ErrorCategory.OperationTimeout, null);
                case BOErrorCode.ObjectNotFound:
                    return new ErrorRecord(ex, Enum.GetName(typeof(BOErrorCode), errorCode), ErrorCategory.ObjectNotFound, null);
                case BOErrorCode.InstanceFailure:
                    return new ErrorRecord(ex, Enum.GetName(typeof(BOErrorCode), errorCode), ErrorCategory.InvalidResult, null);
                case BOErrorCode.NotificationConfigurationError:
                    return new ErrorRecord(ex, Enum.GetName(typeof(BOErrorCode), errorCode), ErrorCategory.InvalidData, null);
                case BOErrorCode.StartServerFailure:
                case BOErrorCode.StopServerFailure:
                case BOErrorCode.ReStartServerFailure:
                    return new ErrorRecord(ex, Enum.GetName(typeof(BOErrorCode), errorCode), ErrorCategory.InvalidOperation, null);
                default:
                    return new ErrorRecord(ex, Enum.GetName(typeof(BOErrorCode), errorCode), ErrorCategory.NotSpecified, null);
            }
        }

    }
}
